
using System;
using System.Collections.Generic;
using Subsync;
using Subsync.Test;

namespace Subsync.CLI
{
	/// <summary>
	/// CLI V2.0
	/// </summary>
	public class Views
	{
		
		Dictionary<string,List<object>> tokens = new Dictionary<string,List<object>>();
		List<KeyValuePair<string,App.initPointer>> dispatcher_triggers = new List<KeyValuePair<string,App.initPointer>>();
		
		List<App> installed_apps = new List<App>();
		
		//tokenize and initialized dispatch.
		public Views (string[] args)
		{
			//INSTALL APPS HERE.
			installed_apps.Add (new HelloWorld());
			installed_apps.Add(new Subsync.Core.Subversion.SVNCheckout());
            installed_apps.Add(new Subsync.Core.Subversion.SVNMkdir());
            installed_apps.Add(new Subsync.Core.Subversion.SVNCommit());
            installed_apps.Add(new Subsync.Core.Subversion.SVNAdminCreate());
            installed_apps.Add(new Subsync.Core.Subversion.SVNAdd());
            installed_apps.Add(new Subsync.Core.Subversion.SVNRevert());
			//INSTALL END
			
			foreach (App app in installed_apps) {
				foreach (KeyValuePair<string, List<object>> match in app.getTokens ()) {
					tokens.Add (match.Key, match.Value);
				}
			}
			
			foreach (App app in installed_apps) {
				//for initdispatchers
				foreach (KeyValuePair<string,App.initPointer> m in app.getInitPointers()) {
					dispatcher_triggers.Add(m);	
				}
			}
			
			//begin dispatch
			Dictionary<string,List<object>> tokenized = Tokenize(args);
			Dispatch(tokenized);
		}
		
		public void Dispatch(Dictionary<string,List<object>> tokenized_args) {
			//loop and find the first match, then dispatch. else load the default method.
			foreach (KeyValuePair<string,App.initPointer> m in dispatcher_triggers) {
				if (tokenized_args.ContainsKey(m.Key)) {
					m.Value(tokenized_args);
					return;
				}
			}
			
			if (installed_apps.Count == 0) { 
				Console.WriteLine("Please install at least 1 module to continue.");
			}
			
			//default dispatch
			this.defaultError();
		}
		
		public Dictionary<string,List<object>> Tokenize(string[] args) {
			
			Dictionary<string,List<object>> ret = new Dictionary<string,List<object>>();
			string current_token_name = null;
			
			foreach (string s in args) {
				
				if (s.ToCharArray()[0] == '-') {
					string token_keyword;
					if (s.ToCharArray()[1] == '-') token_keyword = s.Split('-')[2];
					else token_keyword = s.Split('-')[1];
				
					//lets try to find it in tokens
					foreach (KeyValuePair<string,List<object>> match in tokens) {
						string token_name = match.Key;
						List<object> token_args_list = match.Value;
						if (token_args_list.Contains(token_keyword)) {
							ret.Add(token_name,new List<object>());
							current_token_name = token_name;
						}
					}
				}
				
				else if (current_token_name != null) {
					//the next param is an arg, lets save it.
					ret[current_token_name].Add(s);
					continue;
				}
			}
			return ret;
		}
		
		public void defaultError() {
			Console.WriteLine("TODO.");
		}
	}
	
}
